package cn.iocoder.yudao.module.system.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.userdetail.vo.UserDetailPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.userdetail.vo.UserDetailRespVO;
import cn.iocoder.yudao.module.system.controller.app.user.vo.AppUserCheckReqVO;
import cn.iocoder.yudao.module.system.controller.app.user.vo.AppUserCheckRespVO;
import cn.iocoder.yudao.module.system.convert.user.UserConvert;
import cn.iocoder.yudao.module.system.convert.userdetail.UserDetailConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.userdetail.UserDetailDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.module.system.service.userdetail.UserDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashSet;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 用户")
@RestController
@RequestMapping("/system/user")
@Validated
public class AppUserSystemController {

    @Resource
    private AdminUserService userService;

    @Resource
    private UserDetailService userDetailService;

    @PostMapping("/check")
    @Operation(summary = "检查给定手机号是否注册大愚灸师")
    public CommonResult<AppUserCheckRespVO> checkUser(@Valid @RequestBody AppUserCheckReqVO reqVO) {
        Set<String> mobileList = new HashSet<>();
        for(int i = 0; i<reqVO.getMobileList().size(); i++){
            mobileList.add(reqVO.getMobileList().get(i));
        }

        return success(UserConvert.INSTANCE.convertAppCheck(userService.getUserListByMobile(mobileList)));
    }

    @GetMapping("/page")
    @Operation(summary = "获得艾灸师详情分页")
    public CommonResult<PageResult<UserDetailRespVO>> getUserDetailPage(@Valid UserDetailPageReqVO pageVO) {
        PageResult<UserDetailDO> pageResult = userDetailService.getUserDetailPage(pageVO);
        return success(UserDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/get")
    @Parameter(name = "mobile", description = "手机号码", required = true, example = "186")
    @Operation(summary = "获得艾灸师详情")
    public CommonResult<UserDetailRespVO> getUserDetail(@RequestParam("mobile") String mobile){
        UserDetailDO userDetailDO = userDetailService.getUserDetailByMobile(mobile);
        return success(UserDetailConvert.INSTANCE.convert(userDetailDO));
    }
}
